package com.hzit.pay.web.service.strategy;

import com.hzit.pay.web.config.PayWebConfig;
import com.hzit.pay.web.factory.PayStrategyFactory;
import com.hzit.pay.web.iClient.IAliapyFrontClient;
import com.hzit.pay.web.model.MchInfo;
import com.hzit.pay.web.model.PaySerial;
import com.hzit.pay.web.service.IMchInfoService;
import com.hzit.pay.web.service.IPayStrategyService;
import com.hzit.payweb.api.alipay.AlipayH5Req;
import com.hzit.payweb.api.enums.PayErrorMsgEnum;
import com.hzit.payweb.api.enums.PayWayEnum;
import com.hzit.payweb.api.resp.PayFrontRespData;
import com.hzit.payweb.api.resp.RespVo;
import com.hzit.payweb.api.resp.data.PayRespData;
import com.hzit.payweb.api.utils.PayDigestUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 支付宝H5支付
 */
@Component
public class AliapyH5PayImpl implements IPayStrategyService, InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(AliapyH5PayImpl.class);

    @Autowired
    private IAliapyFrontClient aliapyFrontClient;

    @Autowired
    private PayWebConfig payWebConfig;

    @Autowired
    private IMchInfoService mchInfoService;

    @Override
    public RespVo<PayRespData> pay(PaySerial paySerial) {
        logger.info("支付宝的H5支付");

        AlipayH5Req alipayH5Req = new AlipayH5Req();
        alipayH5Req.setBody(paySerial.getOrderSubject());
        alipayH5Req.setSubject(paySerial.getOrderSubject());
        alipayH5Req.setOutTraderNo(paySerial.getPaySerialNo());
        alipayH5Req.setTotalAmt(paySerial.getPayAmt().toString());
        alipayH5Req.setQuitUrl("http://www.baidu.com"); //TODO 支付中段跳转跳页
        alipayH5Req.setNotifyUrl(payWebConfig.getNotifyUrl());
        alipayH5Req.setProductCode(payWebConfig.getProductCode());

        //签名 TODO feign调用
        RespVo<PayFrontRespData> respVo =  aliapyFrontClient.alipayH5Pay(alipayH5Req);

        if (respVo.getCode() != 0){
            logger.info("订单id：{}调用前置服务失败：{}",paySerial.getPaySerialNo(),respVo.getMsg());
            return RespVo.returnFail(PayErrorMsgEnum.ERROR.getCode(),PayErrorMsgEnum.ERROR.getDesc());
        }
        //TODO 更新交易流水状态为支付中


        MchInfo mchInfo = mchInfoService.getMchInfoByMchId("1000");
        PayFrontRespData respData =  respVo.getData();

        //生成签名
        try {
            String mySign = PayDigestUtil.getSign(respData,mchInfo.getRespKey());
            RespVo<PayRespData> resp = RespVo.returnSuccess("处理成功");
            PayRespData payRespData = new PayRespData();
            payRespData.setSign(mySign);
            payRespData.setPayUrl(respData.getPayUrl());
            payRespData.setOrderId(respData.getOutTradeNo());
            resp.setData(payRespData);
            return resp;

        } catch (IllegalAccessException e) {
            logger.error("IllegalAccessException",e);
            return RespVo.returnFail(PayErrorMsgEnum.SIGN_CREATE_ERROE.getCode(),PayErrorMsgEnum.SIGN_CREATE_ERROE.getDesc());
        }


    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("-------初始化AliapyH5PayImpl-------------");
        PayStrategyFactory.concurrentHashMap.put(PayWayEnum.ALIPAY_WAP_PAY.getCode(),this);

    }
}
